import time

import requests

import utils
from get_book_detail.get_page_detil import get_page_detil

book_id_db = utils.mongo_manager(collect_name="book_id")
book_content=utils.mongo_manager(collect_name="book_content")


while True:
    book_id_info = book_id_db.find_one_and_update({"bookStatus": 11}, {"bookStatus": 12})
    if book_id_info:
        book_id= book_id_info.get('_id')
        page_list=book_id_info.get('chapter_num')
        print(book_id_info.get('title'),book_id,page_list)
        while page_list:
            try:
                i=page_list[0]
            except:
                break
            id = str(book_id) + str(i)  # 对章节去重
            if book_content.not_exist({'_id': id}):
                session = requests.session()
                content = get_page_detil(book_id, i, session)
                session.close()
                state = utils.is_english_or_punctuation(content[:30])
                if 'xml' in content and state==1:
                    page_list.pop(0)
                    try:
                        book_content.insertOne({"_id": id, "book_id": book_id, 'chapter_num': i, 'content': content})
                    except:
                        book_content.updateOne({"_id": id},
                                               {"_id": id, "book_id": book_id, 'chapter_num': i, 'content': content})
                else:
                    break
                time.sleep(3)
            else:
                print("已存在")
                page_list.pop(0)
        else:
            # 整本爬完才改状态
            book_id_info = book_id_db.find_one_and_update({"_id": book_id},{"bookStatus": 10})
            print( book_id_info.get('title'),book_id,'爬取完成')
    else:
        break

book_content.close()
book_id_db.close()
